package com.framework.seedkit.http.volley;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.framework.seedkit.base.BaseApplication;

import org.json.JSONObject;

public class HttpHelper {

	//全局请求队列RequestQueue
	private static RequestQueue requestQueue = Volley.newRequestQueue(BaseApplication.getInstance());

	/*
	* 描述: get方法
	* @parameter url 请求的url
	* @parameter jsonPara JSON形式的请求参数
	* @parameter callback 回调接口
	*
	* @return void
	*/
	public static void get(String url, final HttpJsonCallback callback) {
		get(url, "rongzitong", callback);
	}

	/*
	* 描述: get方法
	* @parameter url 请求的url
	* @parameter jsonPara JSON形式的请求参数
	* @parameter tag App请求的标识
	* @parameter callback 回调接口
	*
	* @return void
	*/
	public static void get(String url, String tag, final HttpJsonCallback callback) {
		if (TextUtils.isEmpty(tag)) {
			tag = "rongzitong";
		}
		final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				callback.onSuccess(response);
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Log.e("", "---------- Error = " + arg0.toString());
				callback.onError();
			}
		});
		jsonRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 2, 1.0f));

		jsonRequest.setTag(tag);
		requestQueue.add(jsonRequest);
	}

	/*
	* 描述: post方法
	* @parameter url 请求的url
	* @parameter jsonPara JSON形式的请求参数
	* @parameter tag App请求的标识
	* @parameter callback 回调接口
	*
	* @return void
	*/
	public static void post(String url, JSONObject jsonPara, String tag, final HttpJsonCallback callback) {
		if (TextUtils.isEmpty(tag)) {
			tag = "rongzitong";
		}

		if (TextUtils.isEmpty(url) || callback == null) {
			Log.e("post", "----------- param error");
			return;
		}

		JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonPara, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				if (response == null) {
					callback.onError();
				} else {
					callback.onSuccess(response);
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				Log.e("", "-------------- onErrorResponse : error = " + error.getMessage());
				callback.onError();
			}
		});

		request.setTag(tag);
		request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 2, 1.0f));
		requestQueue.add(request);
	}

	public static void cancelRequest(String tag) {
		if (TextUtils.isEmpty(tag)) {
			return;
		}
		requestQueue.cancelAll(tag);
	}

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm != null) {
			NetworkInfo info = cm.getActiveNetworkInfo();
			if (info != null) {
				return true;
			}
		}
		return false;
	}

}
